class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class Main {
    public ListNode swapPairs(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode head1 = new ListNode(-1);
        ListNode cur = head.next;
        ListNode prev = head;
        ListNode cur1 = head1;
        ListNode curNext = head;
        while(curNext != null && curNext.next != null){
            curNext = cur.next;
            cur1.next = cur;
            cur1 = cur1.next;
            cur1.next = prev;
            cur1 = cur1.next;
            if(curNext == null){
                break;
            }
            cur = curNext.next;
            prev = curNext;

        }
        if(curNext == null){
            cur1.next = null;
        }else{
            cur1.next = curNext;
        }

        return head1.next;
    }
}